import { IData } from '@amcax/base';
import { IDocument } from '@app-nextmesh/types';

/**
 * 全局文档信息管理，文档信息中包含左侧树的信息；
 * 和一些能被经常用到的数据，比如：几何id列表、网格id列表
 */
class DocumentManager {
  private documentInfo: IDocument = null;

  public setDocumentInfo(docInfo: IDocument) {
    this.documentInfo = docInfo;
  }

  /**
   * 前端显示和维护的是Component，对应到后端的geometry（后期应该会迭代更新掉）
   * @returns Component 信息数组，每个Component中可以包含多个几何和多个网格
   */
  public getComponents() {
    return this.documentInfo.geometries ?? [];
  }

  public getGeometriesId() {
    return Array.from(this.documentInfo?.geometriesId ?? new Set<string>());
  }

  public setGeometriesId(geometriesId: string[]) {
    this.documentInfo.geometriesId = new Set(geometriesId);
  }

  public getGeometriesData() {
    return this.documentInfo.geometriesData ?? [];
  }

  public getMeshesId() {
    return Array.from(this.documentInfo.meshesId ?? new Set<string>());
  }

  public setMeshesId(meshesId: string[]) {
    this.documentInfo.meshesId = new Set(meshesId);
  }

  public addMeshesId(meshesId: string[]) {
    this.documentInfo.meshesId = new Set([
      ...(this.documentInfo.meshesId ?? []),
      ...meshesId,
    ]);
  }

  public getMeshesData() {
    return this.documentInfo.meshesData ?? [];
  }

  public setMeshesData(meshesData: IData[]) {
    this.documentInfo.meshesData = meshesData;
  }
}

export const documentManager = new DocumentManager();
